package cn.edu.nju.software.android.adapter;

import java.util.ArrayList;

import me.maxwin.view.IListviewCallback;

import cn.edu.nju.software.android.activity.MainActivity;
import cn.edu.nju.software.android.activity.R;
import cn.edu.nju.software.android.model.User;
import cn.edu.nju.software.android.util.AsyncImageLoader;
import cn.edu.nju.software.android.util.ImageUtil;
import cn.edu.nju.software.android.util.LocalFileHelper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class UserListViewAdapter extends ArrayAdapter<User> {
	private Context context;
	private IListviewCallback callback;
	private User user;
	private ArrayList<User> users = new ArrayList<User>();
	private String fileName;
	private static final String FRIENDLISTFILENAME = "friendlistfile";
	private static final String FOLLOWERLISTFILENAME = "followerlistfile";
	private int type ;
	public static final int FOLLOWER = 1;
	public static final int FRIEND = 2;

	private Handler failureHandler = new Handler(){
		public void handleMessage(Message msg) {
			Toast.makeText(context, "WIFI没有打开", Toast.LENGTH_SHORT).show();
		}
	};
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			notifyDataSetChanged(); // 发送消息通知ListView更新
			try{
				callback.onComplete();
			}catch(Exception e){
				e.printStackTrace();
			}
			if((type == FOLLOWER && users.size() >= user.getFollowersCount()) || (type == FRIEND && users.size() >= user.getFriendsCount())){
				callback.disableLoadFunction();
			}else{
				callback.enableLoadFunction();
			}
		}
	};

	@SuppressWarnings("unchecked")
	public UserListViewAdapter(Context context, IListviewCallback callback, User user, int type) {
		super(context, R.layout.listview_user);
		this.context = context;
		this.type = type;
		this.user = user;
		this.callback = callback;
		if(type == FRIEND)
			fileName = FRIENDLISTFILENAME;
		else if(type == FOLLOWER)
			fileName = FOLLOWERLISTFILENAME;

		ArrayList<User> loadedFile = (ArrayList<User>)LocalFileHelper.loadObjectsListFromFile(context, fileName);
		if(loadedFile != null)
			users = loadedFile;
		else{
			getMoreUsers(MainActivity.uid);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		convertView = inflater.inflate(R.layout.listview_user, parent, false);
		ImageView userImageView = (ImageView) convertView
				.findViewById(R.id.userImage);
		TextView userNameView = (TextView) convertView
				.findViewById(R.id.userName);

		User user = users.get(position);
		AsyncImageLoader userImageAsyLoader = new AsyncImageLoader();
		Drawable userCachedImage = userImageAsyLoader.loadDrawable(context,failureHandler,
				user.getProfileImageUrl(), userImageView,
				new AsyncImageLoader.ImageCallback() {
			@Override
			public void imageLoaded(Drawable imageDrawable,
					ImageView imageView, String imageUrl) {
				if(imageDrawable != null)
					imageView.setImageDrawable(ImageUtil.resizeProfilePic(imageDrawable));
			}
		},100, 100);
		if (userCachedImage == null) {
			userImageView.setImageResource(R.drawable.user);
		} else {
			userImageView.setImageDrawable(ImageUtil.resizeProfilePic(userCachedImage));
		}
		userNameView.setText(user.getName());
		return convertView;
	}

	@Override
	public int getCount() {
		return users.size();
	}

	@Override
	public User getItem(int position) {
		return users.get(position);
	}


	public void getMoreUsers(long uid){
		switch(type){
		case FOLLOWER:
			User.downloadFollowersList(uid, users.size(), handler, users);
			break;
		case FRIEND:
			User.downloadFriendsList(uid, users.size(), handler, users);
			break;
		}
	}

	public void saveUsers(){
		LocalFileHelper.saveObjects(context, users, fileName);
	}
}
